<fieldset class="unfoldable">
  <%= form.input :name, required: true %>
  <%= form.input :permalink, required: true if form.object.persisted? %>
  <%= form.input :notify_email_address,
        label: "Notify after deploy",
        help: "Email addresses to notify of finished deploys, separated by <code>;</code>".html_safe
  %>

  <% if DeployGroup.enabled? %>
    <%= render 'deploy_groups/deploy_group_select', form: form %>
  <% else %>
    <%= form.input :production, as: :check_box,
          label: 'Impacts production',
          help: "Deploys to production need to pass a buddy check when BuddyCheck is enabled."
    %>
  <% end %>

  <%
    confirm_label = "Confirm before deployment"
    no_code_label = "Does not deploy code"
    no_ref_label = "When selected with '#{no_code_label}' and disabled '#{confirm_label}' the stage can be directly executed."
  %>

  <% help = "Bypass " + [("buddy check" if BuddyCheck.enabled?), "release tracking"].compact.to_sentence %>
  <%= form.input :no_code_deployed, as: :check_box, label: no_code_label, help: help %>

  <%= form.input :run_in_parallel, as: :check_box, label: "Can run in parallel", help: "Deploys are not queued. Executed immediately" %>

  <%= form.input :cancel_queued_deploys, as: :check_box, label: "Max 1 queued deploy per user", help: "When a new deploy is created for a user. Any queued deploys for that user are cancelled. This most useful when trying to not deploy every push for frequently updated projects." %>

  <%= form.input :confirm, as: :check_box, label: confirm_label, help: "Show a review page before starting a deploy" %>

  <%= form.input :no_reference_selection, as: :check_box, label: "Disable reference selection", help: no_ref_label %>

  <% if interval = Samson::Periodical.interval(:periodical_deploy) %>
    <%= form.input :periodical_deploy, as: :check_box, help: "Deploy every #{distance_of_time_in_words(interval)} if last deploy succeeded, enable automated deploy failure email to be alerted " %>
  <% end %>

  <% if @project.releases.any? %>
    <%= form.input :deploy_on_release, as: :check_box, label: "Automatically deploy new releases" %>
  <% end %>

  <% if DeployGroup.enabled? %>
    <%= form.input :is_template, as: :check_box,
          label: "Template Stage",
          help: "This stage can serve as a template to create new stages for its environment (only changing the deploy group)."
    %>
  <% end %>

  <%= form.input :builds_in_environment, as: :check_box, label: "Builds available in environment", help: "Useful for manual docker deploys or artifact extraction, format is BUILD_FROM_Dockerfile=" %>

  <%= Samson::Hooks.render_views(:stage_form_checkbox, self, form: form) %>
</fieldset>

<%= render 'commands', form: form %>
<%= render 'shared/dashboard_field', form: form %>
<%= render 'deploy_failure_emails', form: form %>
<%= Samson::Hooks.render_views(:stage_form, self, form: form) %>

<script>
  // fold empty fieldsets and let users unfold them to reduce clutter
  // except for the attributes and command fieldsets
  $(function(){
    $("fieldset").not(".unfoldable").each(function(_, fieldset){
      var fieldset = $(fieldset);
      var form_elements = fieldset.find('> *').not('legend');

      var filled = fieldset.find(':input').filter(function(_, el){
        if(el.type == "checkbox" || el.type == "radio") {
          return $(el).is(':checked');
        } else if(el.type == "hidden") {
          return false;
        } else {
          return $(el).val() != "";
        }
      });

      if(filled.size() == 0) {
        fieldset.find('legend').
          click(function(){ form_elements.toggle() }).click().
          css('cursor', 'pointer').
          append(' &#x2304;');
      }
    });
  });
</script>
